package indi.ui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;

import indi.entity.Manager;
import indi.service.BookSystem;

public class MainFrame extends JFrame{

	//数据
	BookSystem currentBookSystem;
	private ArrayList<Record> recordList;
	private ArrayList<Record> recordToQuery;
	private CardLayout card;
	private JPanel pn, pRT, pBook, pQR, pRG;
	private JPanel pRtButton,pInput;
	private JTextField txtContent;
	private JTabbedPane showPane,tpGather;
	private JMenuBar menuBar;
	private JMenu menuFile,menuOperate,menuHelp;
	private JMenuItem rtGather,queryGather,miShow,miExit,miCheck,miAbout;
	private JToolBar toolBar;
	private JButton btnBR, btnRT, btnQR, btnRG, btnRest;
	private JTextField txtBookId;// 结束界面图书ISBN
	private JTextField txtBookIdOfReturning;// 还书界面图书ISBN
	private JTextField txtLiscenceOfBorrowing;// 结束界面借阅证输入
	private JTextField txtLiscenceOfReturning;// 还书界面借阅证输入
	private JTextField txtLiscenceOfQuery;// 查询界面借阅证
	private JTextField txtName;// 注册界面姓名
	private JTextField txtAcadmy;// 注册界面系别
	
	//构造函数
	public MainFrame(Manager currentManager){
		//构造函数
		super("图书管理系统");
		//设置窗体icon
		ImageIcon icon = new ImageIcon("images\\BookManager.");
		this.setIconImage(icon.getImage());
		//列表，业务对象初始化
		recordList = new ArrayList<Record>();//所有借书记录
		recordToQuery = new ArrayList<Record>();//所有查询的记录
		//图书管理系统逻辑类构造
		currentBookSystem = new BookSystem(currentManager);
		
		//初始化菜单
		initMenu();
		//初始化工具栏
		initToolBar();
		//设置主面板为CardLayout卡片布局
		card = new CardLayout();//卡片布局
		pn = new JPanel();//重建一个面板
		pn.setLayout(card);//设置卡片布局
		//数据采集的选项卡面板
		tpGather = new JTabbedPane(JTabbedPane.TOP);
		/*
		tpGather.add("借书", new JScrollPane());
		tpGather.add("还书", new JScrollPane());
		tpGather.add("注册", new JScrollPane());
		*/
		//生成“借书记录”显示的选项卡面板
		showPane = new JTabbedPane(JTabbedPane.TOP);
		showPane.addTab("借书记录", new JScrollPane());//可滚动表
		
		//将两个选项卡面板添加到卡片面板中
		pn.add(tpGather, "gather");
		pn.add(showPane, "show");
		//将主面板添加到窗体中
		getContentPane().add(pn, BorderLayout.CENTER);
		//初始化“借书”数据采集界面
		initBorrowBookGUI();
		//初始化“还书”数据采集界面
		initReturnBookGUI();
		//初始化“查询”数据采集界面
		initQueryBookGUI();
		//初始化“注册”数据采集界面
		initRegisteLiscenceGUI();
		
		//设置窗体出示可见
		this.setVisible(true);
		//设置窗体初始化最大化
		this.setSize(600, 400);
		//设置窗体初始化居中
		this.setLocationRelativeTo(null);
		//设置默认关闭模式为“退出程序”
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		//其他操作
		new UpdateTableThread().start();
		
	}
	

	//清除所有
	public void clearAll() {
		
	}
	
	//初始化菜单的方法
	private void initMenu() {
		//初始化菜单组件
		menuBar = new JMenuBar();
		this.setJMenuBar(menuBar);
		menuFile = new JMenu("文件");
		menuBar.add(menuFile);
		
		menuOperate = new JMenu("操作");
		menuBar.add(menuOperate);
		
		JMenuItem brGather = new JMenuItem("借阅");//注意美观
		brGather.addActionListener(new BorrowBookListener());//注册监听
		menuOperate.add(brGather);
	
		rtGather = new JMenuItem("还书");
		rtGather.addActionListener(new ReturnBookListener());
		menuOperate.add(rtGather);
		
		queryGather = new JMenuItem("查询");
		queryGather.addActionListener(new QueryBookListener());
		menuOperate.add(queryGather);
		
		miShow = new JMenuItem("注册");
		miShow.addActionListener(new ShowRecordListener());
		menuOperate.add(miShow);
		
		//添加分割线
		menuOperate.addSeparator();
		
		//退出系统按钮
		miExit = new JMenuItem("退出系统");
		miExit.addActionListener(new
				ActionListener() {
					public void actionPerformed(ActionEvent e) {
						//显示确认对话框当选择YES_OPTION是退出系统
						if(JOptionPane.showConfirmDialog(null, "您确认退出系统！",
								"推出系统",JOptionPane.YES_NO_OPTION) == 
								JOptionPane.YES_NO_OPTION) {
							//退出系统
							System.exit(0);
						}
					}
		});
		menuOperate.add(miExit);
		
		menuHelp = new JMenu("帮助");
		menuBar.add(menuHelp);
	
		miCheck = new JMenuItem("查看帮助");
		/*
		 * 注册监听
		 */
		menuHelp.add(miCheck);
		
		miAbout = new JMenuItem("关于系统");
		/*
		 * 注册监听
		 */
		menuHelp.add(miAbout);
		
	}
	
	//初始化菜单栏
	public void initToolBar() {
		//创建工具栏
		toolBar = new JToolBar();
		toolBar.setOrientation(SwingConstants.VERTICAL);
		//toolBar.setLayout(new FlowLayout(FlowLayout.CENTER));
		toolBar.setSize(100, 300);
		//见工具栏添加到窗体北边
		getContentPane().add(toolBar, BorderLayout.WEST);
		
		//添加带有图表的工具栏按钮
		//借书采集
		ImageIcon gatherIconBR = new ImageIcon();//("images\\gatherData.");
		btnBR = new JButton("借书", gatherIconBR);
		//btnBR.setSize(150, 150);
		//btnBR.setBounds(new Rectangle(100,200));
		btnBR.addActionListener(new BorrowBookListener());
		toolBar.add(btnBR);
		//还书采集
		ImageIcon rtIcon = new ImageIcon("images\\returnBook.");
		btnRT = new JButton("还书", rtIcon);
		btnRT.addActionListener(new ReturnBookListener());
		toolBar.add(btnRT);
		//查询采集
		ImageIcon qrIcon = new ImageIcon("images\\queryBook.");
		btnQR = new JButton("查询", qrIcon);
		btnQR.addActionListener(new QueryBookListener());
		toolBar.add(btnQR);
		//注册
		ImageIcon rgIcon = new ImageIcon("images\\registeLiscence");
		btnRG = new JButton("注册", rgIcon);
		btnRG.addActionListener(new RegisteLiscenceListener());
		toolBar.add(btnRG);
	}
	
	//初始化借书GUI
	public void initBorrowBookGUI() {
		JPanel pLog = new JPanel();
		tpGather.addTab("借书", pLog);
		pLog.setLayout(new BoxLayout(pLog, BoxLayout.Y_AXIS));//?
		//pLog.setLayout(new FlowLayout(FlowLayout.LEADING));
		
		//书本
		JPanel pBook = new JPanel();
		pLog.add(pBook);
		pBook.setLayout(new FlowLayout(FlowLayout.CENTER, 5,5));
		
		JLabel lblBookId = new JLabel("ISBN:");
		pBook.add(lblBookId);
		
		txtBookId = new JTextField();// 作为全局
		txtBookId.setPreferredSize(new Dimension(100, 25));
		pBook.add(txtBookId);
		
		//借书证
		JPanel pLiscence = new JPanel();
		pLog.add(pLiscence);
		
		JLabel lblLiscence = new JLabel("借阅证号：");
		pLiscence.add(lblLiscence);
		
		txtLiscenceOfBorrowing = new JTextField();
		txtLiscenceOfBorrowing.setPreferredSize(new Dimension(100, 25));
		pLiscence.add(txtLiscenceOfBorrowing);
		
		//按钮
		JPanel pLogButton = new JPanel();
		pLog.add(pLogButton);
		
		JButton btnLogConfirm = new JButton("确认借书");
		//添加监听器
		btnLogConfirm.addActionListener(new BorrowBookListener());
		pLogButton.add(btnLogConfirm);
		
		JButton btnLogReset = new JButton("重置");
		//添加监听器
		btnLogReset.addActionListener(new ResetListener());
		pLogButton.add(btnLogReset);
	}
	
	//初始化还书GUI
	public void initReturnBookGUI() {
		JPanel pRT = new JPanel();
		tpGather.addTab("还书", pRT);
		pRT.setLayout(new BoxLayout(pRT, BoxLayout.Y_AXIS));
		//pRT.setLayout(new FlowL)
		
		//图书号
		JPanel pBook = new JPanel();
		pRT.add(pBook);
		pBook.setLayout(new FlowLayout(FlowLayout.CENTER, 5,5));
		
		JLabel lblBookId = new JLabel("ISBN:");
		pBook.add(lblBookId);
		
		JTextField txtBookId = new JTextField();
		txtBookId.setPreferredSize(new Dimension(100, 25));
		pBook.add(txtBookId);
		
		//按钮
		JPanel pRtButton = new JPanel();
		pRT.add(pRtButton);
		
		JButton btnConfirm = new JButton("确认");
		pRtButton.add(btnConfirm);
		
		JButton btnReset = new JButton("重置");
		pRtButton.add(btnReset);
	}
	
	//初始化查询GUI
	public void initQueryBookGUI() {
		pQR = new JPanel();
		tpGather.addTab("查询", pQR);
		pQR.setLayout(new BoxLayout(pQR, BoxLayout.Y_AXIS));
		
		//加一个图像
		
		//输入框
		JPanel pInput = new JPanel();
		pQR.add(pInput);
		
		JTextField txtContent = new JTextField();
		txtContent.setPreferredSize(new Dimension(200, 25));
		pInput.add(txtContent);
		
		//变为回车查询
		
		//显示结果界面，新窗口
		
	}
	
	//初始化注册GUI
	public void initRegisteLiscenceGUI() {
		pRG = new JPanel();
		tpGather.addTab("注册", pRG);
		pRG.setLayout(new BoxLayout(pRG, BoxLayout.Y_AXIS));
		
		//姓名
		JPanel pName = new JPanel();
		pRG.add(pName);
		
		JLabel lblName = new JLabel("姓名:");
		pName.add(lblName);
		
		txtName = new JTextField();
		txtName.setPreferredSize(new Dimension(100, 25));
		pName.add(txtName);
		
		//系别
		JPanel pAcadmy = new JPanel();
		pRG.add(pAcadmy);
		
		JLabel lblAcadmy = new JLabel("系别:");
		pAcadmy.add(lblAcadmy);
		
		txtAcadmy = new JTextField();
		txtAcadmy.setPreferredSize(new Dimension(100, 25));
		pAcadmy.add(txtAcadmy);
		
		//按钮
		JPanel pButton = new JPanel();
		pRG.add(pButton);
		
		JButton btnConfirm = new JButton("确认");
		btnConfirm.addActionListener(new RegisteLiscenceListener());//添加监听器
		pButton.add(btnConfirm);//注意书写
		
		JButton btnReset = new JButton("重置");
		btnReset.addActionListener(new ResetListener());//添加监听器
		pButton.add(btnReset);
	}
	
	//初始化删除GUI
	
	//借书按钮监听器
	public class BorrowBookListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			try {
				// 提取数据
				int liscenceId = Integer.parseInt(txtBookId.getText().trim());
				String isbn = txtLiscenceOfBorrowing.getText().trim();
				// 调用BookSystem.borrowBook()
				currentBookSystem.borrowBook(liscenceId, isbn);
				// 返回信息
				JOptionPane.showMessageDialog(null, "借书成功！");
			}catch(Exception ex) {
				ex.printStackTrace();
				JOptionPane.showMessageDialog(null, "结束失败！", 
						"提示", JOptionPane.INFORMATION_MESSAGE);
			}
		}
		
	}
	/*
	//结束功能监听器
	public class BorrowBookListener implements ActionListener{
		
		@Override
		public void actionPerformed(ActionEvent e) {
			
		}
	}
	*/
	
	//还书页面监听器
	public class ReturnBookListener implements ActionListener{
		
		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				// 提取数据
				String isbn = txtBookIdOfReturning.getText().trim();
				// 调用系统还书功能
				currentBookSystem.returnBook(isbn);
				// 返回信息
				JOptionPane.showMessageDialog(null, "还书成功！");
			}catch(Exception ex) {
				JOptionPane.showMessageDialog(null, "还书失败！", 
						"提示", JOptionPane.INFORMATION_MESSAGE);
				ex.printStackTrace();
				
			}
		}
	}
	
	//查询借书记录页面监听器
	public class QueryBookListener implements ActionListener{
		
		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				// 提取数据
				int liscenceId = Integer.parseInt(
						txtLiscenceOfReturning.getText().trim());
				// 调用系统查询图书
				currentBookSystem.queryRecords(liscenceId);
				// 输出返回数据
			}catch(Exception ex) {
				ex.printStackTrace();
			}
		}
	}
	
	//注册页面监听器
	public class RegisteLiscenceListener implements ActionListener{
		
		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				// 提取信息
				String name = txtName.getText().trim();
				String acadmy = txtAcadmy.getText().trim();
				// 调用系统注册功能
				currentBookSystem.registeLiscence(name, acadmy);
				// 提示信息
				JOptionPane.showMessageDialog(null, "注册成功！");
			}catch(Exception ex) {
				JOptionPane.showMessageDialog(null, "注册失败！", 
						"提示", JOptionPane.WARNING_MESSAGE);
				ex.printStackTrace();
			}
		}
	}
	
	//显示查询信息监听器
	public class ShowRecordListener implements ActionListener{
		
		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				// 提取信息
				int liscenceId = Integer.parseInt(txtLiscenceOfQuery.getText().trim());
				// 调用系统查询借阅信息功能
				currentBookSystem.queryRecords(liscenceId);
				// 用一个表格显示查询信息
				
			}catch(Exception ex) {
				JOptionPane.showMessageDialog(null, "查询失败", 
						"提示", JOptionPane.WARNING_MESSAGE);
				ex.printStackTrace();
			}
		}
	}
	
	//重置监听器
	public class ResetListener implements ActionListener {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			
		}
	}
	//更新线程类
	public class UpdateTableThread {

		public void start() {
			// TODO Auto-generated method stub
			
		}

	}

	
	//刷新借书记录数据
	public void flushRecordTable() {
		
	}

	public static void main(String[] a) {
		new MainFrame();
	}

}
